package com.dao.airticle;

import com.mapper.ArticleMapper;
import com.pojo.Article;
import com.pojo.Author;
import com.pojo.Column;
import com.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ColumnDao {
    /**
     *新建专栏
     */
    public static void add_column(String column_name,String userid) throws IOException {
        System.out.println("[info]新建专栏");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        articleMapper.add_column(column_name,userid);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }

    /**
     * 获取专栏
     */
    public static List<String> get_column_list(String userid)throws IOException{
        System.out.println("[info]获取所有专栏");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
         List<String>column_list=new ArrayList<>();
         column_list=articleMapper.get_column_list(userid);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
        return column_list;
    }

    /**
     * 获取当前文章专栏
     */
    public static List<String> get_blog_column(String blog_id)throws IOException{
        System.out.println("[info]获取当前文章专栏");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        List<String>column_list=new ArrayList<>();
        column_list=articleMapper.get_current_column(blog_id);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
        return column_list;
    }

    /**
     * 修改专栏名
     */
    public static void change_column_name(String new_column_name,String old_column_name)throws IOException{
        System.out.println("[info]编辑专栏");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        articleMapper.change_column_name(new_column_name, old_column_name);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }

    /**
     * 修改博客所对应的专栏名
     */
    public static void change_article_column(String new_column_name,String old_column_name)throws IOException{
        System.out.println("[info]修改博客专栏");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        articleMapper.change_article_column(new_column_name, old_column_name);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }

    /**
     * 删除专栏
     */
    public static void del_column(String column_name)throws IOException{
        System.out.println("[info]删除专栏");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        articleMapper.del_column(column_name);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }

    /**
     * 删除博客所对应的专栏
     */
    public static void del_article_column(String column_name)throws IOException{
        System.out.println("[info]删除博客的专栏");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        articleMapper.del_article_column(column_name);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }

    /**
     * 获取专栏中的博客
     */
    public static List<Article> get_column_article(String column_name)throws IOException{
        System.out.println("[info]获取专栏中的博客");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        List<Article>articles=new ArrayList<>();
        articles=articleMapper.get_column_article(column_name);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
        return articles;
    }

    /**
     * 移除专栏中的博客
     */
    public static void del_column_blog(String blog_id,String column_name)throws IOException{
        System.out.println("[info]移除专栏中的博客");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        articleMapper.del_column_blog(blog_id,column_name);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }

    /**
     * 移除博客的所有专栏
     */
    public static void del_blog_column(String blog_id)throws IOException{
        System.out.println("[info]移除博客的所有专栏");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        articleMapper.del_blog_column(blog_id);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }

    /**
     * 获取专栏的作者信息
     */
    public static Author get_column_author(String column_name,String self_id)throws IOException{
        System.out.println("[info]获取专栏的作者信息");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        Author author=articleMapper.get_column_author(column_name,self_id);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
        return author;
    }

    /**
     * 专栏查重
     */
    public static boolean check_column(String column_name)throws IOException{
        System.out.println("[info]专栏查重");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
         Column column=articleMapper.check_column(column_name);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();

        return column == null;
    }

    /**
     * 获取作者的专栏
     */
    public static List<Column> get_author_column(String column_author)throws IOException{
        System.out.println("[info]获取作者的专栏");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        List<Column>columns=articleMapper.get_author_column(column_author);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
        return columns;
    }

}
